﻿var BibleSearch = {
    /**
     * 获取目录数据
     * @param id 目录Id
     * @returns {Array} 目录数据
     */
    searchCatalog: function (startId,endId) {
        return Catalog.filter(function (item) {
            return item.Id >= startId && item.Id <= endId;
        });
    },
    /**
     * 查询指定章
     * @param bookId 卷
     * @param chapterId 章
     * @returns {Array}
     */
    searchScripture: function (bookId, chapterId) {
        return Bible.Content.filter(function (item) {
            return item.Book == bookId && item.Chapter == chapterId;
        });
    },
    /**
     * 查询指定章节
     * @param bookId 卷
     * @param chapterId 章
     * @param verseStart 开始节
     * @param verseEnd 结束节
     * @returns {Array}
     */
    searchScripture2: function (bookId, chapterId, verseStart, verseEnd) {
        verseEnd = verseEnd ? verseEnd < verseStart ? verseStart: verseEnd : verseStart;
        return Bible.Content.filter(function (item) {
            return item.Book == bookId && item.Chapter == chapterId && item.Verse >= verseStart && item.Verse <= verseEnd;
        });
    },
    /**
     * 全文查询
     * @param keyword 查询关键字
     * @returns {Array}
     */
    searchContent: function (keyword) {
        return Bible.Content.filter(function (item) {
            return item.Scripture.indexOf(keyword) > -1;
        });
    },
    /**
     * 处理查询的结果，加上卷名
     * @param arr 要处理的数组
     */
    processSearchResult: function (arr) {
        arr.forEach(function (value, index, array) {
            value.Verse = Catalog[value.Book - 1].Short + value.Chapter + ":" + value.Verse;
            array[index] = value;
        });
        return arr;
    }
};

var app = new Vue({
    el: '#app',
    data: {
        theme: 'light',
        hymnModel:false,
        editorModel:false,
        hymn:'',
        keyword: '',
        bookId: 1,
        scroll:false,
        book: Catalog[0],
        chapterId: 1,
        verseStart: '',
        verseEnd: '',
        verses: [],
        oldTestamentCatalog: BibleSearch.searchCatalog(1,39),
        newTestamentCatalog: BibleSearch.searchCatalog(40,66)
    },
    created: function () {
        this.verses = BibleSearch.searchScripture(1, 1);
    },
    methods: {
        render: function (model) {
            this.verses.length = 0;
            this.verses = model ? BibleSearch.searchScripture(this.bookId, this.chapterId) :
                BibleSearch.searchScripture2(this.bookId, this.chapterId, this.verseStart, this.verseEnd);
        },
        onSelectBook: function (name) {
            var index = parseInt(name);
            this.book = Catalog[index - 1];
            this.bookId = index;
            this.chapterId = 1;
            this.verseStart = '';
            this.verseEnd = '';
            this.render(1);
        },
        onChangeChapter: function (index) {
            this.chapterId = index;
            this.verseStart = '';
            this.verseEnd = '';
            this.render(1);
        },
        onChangeVerse: function (index) {
            this.render(0);
        },
        onSearch: function () {
            if (this.keyword) {
                this.verses.length = 0;
                this.verses = BibleSearch.processSearchResult(BibleSearch.searchContent(this.keyword));
            } else {
                this.verseStart ? this.render(0) : this.render(1);
            }
        },
        onIncrease:function(){
            previewWin && previewWin.fontSize(1);
        },
        onLessen:function(){
            previewWin && previewWin.fontSize(-1);
        },
        onScroll:function(){
            this.scroll=!this.scroll;
            previewWin && previewWin.autoScroll(this.scroll);
        },
        onToolBarClick: function (name) {
            switch (name)
            {
                case "引用经文":
                    this.verses.forEach(function(value,index,array){
                        referenceVerse.push("<h3>" + Catalog[value.Book - 1].Short + 
                        value.Chapter + ":" + 
                        value.Verse + 
                        value.Scripture +
                        "</h3>");
                    });
                    break;
                case "查看引用":
                    editor.txt.html("<h1>引用经文</h1>"+referenceVerse.join(''));
                    this.editorModel=true;
                    break;
                case "清空引用":
                    referenceVerse.length=0;
                    break;
                case "通知":
                    this.editorModel=true;
                    break;
                case "赞美诗":
                    this.hymnModel=true;
                    break;
                case "关闭":
                    previewWin && previewWin.close(true), previewWin = null;
                    break;
                default:
                    playImage(name);
                    break;
            }
        },
        onPlayVerse: function () {
            openPreview(playVerse);
        },
        onPlayEditor:function(){
            openPreview(playHtml);
        },
        onPlayHymn:function(){
            openItem(this.hymn);
        }
    }
});
(function(){
    //引用的经文
    window.referenceVerse=[];
    window.editor = new wangEditor('#editor');
    // 自定义菜单配置
    editor.customConfig.menus = [
        'head',  // 标题
        'foreColor',  // 文字颜色
        'backColor',  // 背景颜色
        'list',  // 列表
        'justify',  // 对齐方式
        'quote',  // 引用
        'image',  // 插入图片
        'table',  // 表格
        'video',  // 插入视频
        'undo',  // 撤销
        'redo'  // 重复
    ];
    editor.customConfig.uploadImgShowBase64 = true;
    editor.create();
})();